
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doghost.controllers;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import doghost.DoghostApp;
import doghost.editors.UfEditor;
import doghost.entities.Uf;

/**
 *
 * @author duo
 */
public class UfController {
    protected Uf uf;
    protected boolean operationOk;

    public Uf getUf() {
        return uf;
    }

    public void setUf(Uf uf) {
        this.uf = uf;
    }

    /**
     * Get the value of operationOk
     *
     * @return the value of operationOk
     */
    public boolean isOperationOk() {
        return operationOk;
    }

    /**
     * Set the value of operationOk
     *
     * @param operationOk new value of operationOk
     */
    public void setOperationOk(boolean operationOk) {
        this.operationOk = operationOk;
    }

    public void newUf(EntityManager entityManager, List list) {
        // create a transient object
        this.setOperationOk(false);
        setUf(new Uf());
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        UfEditor ufEditor = new UfEditor(mainFrame, true);
        ufEditor.setLocationRelativeTo(mainFrame);
        ufEditor.setCurrentRecord(getUf());
        ufEditor.setVisible(true);
        if (ufEditor.isSaveFired()) {
            try {
                entityManager.getTransaction().begin();
                entityManager.persist(getUf());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                if(list != null) {
                    list.add(getUf());
                }
                Logger.getLogger(this.getClass().getName()).
                        log(Level.INFO, "Uf adicionado(a) com sucesso: " +
                        getUf());
            }
            catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                        log(Level.SEVERE, "Erro adicionando Uf: " + e);
            }
        }
    }

    public void editUf(EntityManager entityManager, List list, int index) {
        this.setOperationOk(false);
        entityManager.getTransaction().begin();
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        UfEditor ufEditor = new UfEditor(mainFrame, true);
        ufEditor.setLocationRelativeTo(mainFrame);
        ufEditor.setCurrentRecord(getUf());
        ufEditor.setVisible(true);
        if (ufEditor.isSaveFired()) {
            try {
                entityManager.merge(getUf());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                Logger.getLogger(this.getClass().getName()).
                    log(Level.INFO, "Uf Editado(a) com sucesso: " +
                    getUf());
            } catch (RollbackException rex) {
                // refresh uf in list
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Uf erro de edição (rollback) : " +
                    rex);
                Uf originalUf = (Uf)
                        entityManager.find(getUf().getClass(),
                        getUf().getId());
                if(list != null) {
                    list.remove(getUf());
                    list.add(index, originalUf);
                }
            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                        log(Level.SEVERE, "Uf erro de edição: " + e);
                if (entityManager.getTransaction().isActive()) {
                    try {
                        entityManager.getTransaction().rollback();
                    } catch (Exception erb) {
                        Logger.getLogger(this.getClass().getName()).
                                log(Level.SEVERE, "Erro abandonando edição de uf, " +
                                "transação ativa: " + erb);
                    }
                }
            }
        } else {
            try {
                if (entityManager.getTransaction().isActive()) {
                    entityManager.refresh(getUf());
                    entityManager.getTransaction().commit();
                }
                // refresh uf in list
                Uf originalUf = (Uf)
                        entityManager.find(getUf().getClass(),
                        getUf().getId());
                if(list != null) {
                    list.remove(getUf());
                    list.add(index, originalUf);
                }
                Logger.getLogger(this.getClass().getName()).
                    log(Level.INFO, " Edição de Uf cancelada com sucesso " +
                    "uf: " + getUf());
            } catch (RollbackException rex) {
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Erro cancelando edição de uf " +
                    "(rollback) : " + rex);
                if (entityManager.getTransaction().isActive()) {
                    try {
                        entityManager.getTransaction().rollback();
                    } catch (Exception erb) {
                        Logger.getLogger(this.getClass().getName()).
                                log(Level.SEVERE, "Erro abandonando eliminação de uf. " +
                                "transação ativa: " + erb);
                    }
                }
            }
        }
    }

    public void deleteUf(EntityManager entityManager, List list, int index) {
        this.setOperationOk(false);
        Object[] options = {"Sim", "Não"};
        int n = JOptionPane.showOptionDialog(null, "Remove a linha ?", "Aviso",
                JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, null);
        if (n ==JOptionPane.YES_OPTION) {
            try {
                entityManager.getTransaction().begin();
                entityManager.remove(getUf());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                if(list != null) {
                    list.remove(getUf());
                }
            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Erro eliminando uf: " + e);
            }
        }
    }


}
